/**
Copyright (c) 2013 cnova

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

library Collection

    interface Collection
        readonly static hashtable containsTable = InitHashtable()
        method iterator takes nothing returns Iterator
        method add takes integer i returns boolean defaults false
    endinterface
    
    function interface CompareFunction takes integer first, integer second returns boolean
    
    struct Iterator
        private static integer next_index = 1
        boolean end
        static method create takes nothing returns thistype
            local thistype this = thistype.next_index
            set thistype.next_index = thistype.next_index + 1
            if thistype.next_index > 8100 then
                set thistype.next_index = 1
            endif
            return this
        endmethod
        stub method next takes nothing returns integer
            return 0
        endmethod
        stub method terminate takes nothing returns nothing
        endmethod
    endstruct
    
    module IteratorModule 
        method next takes nothing returns integer
            local integer rval 
            if .curr != 0 then
                set rval = .curr.data
                set .curr = .curr.next
                set .end = false
                return rval
            else
                set .end = true
                return 0
            endif
        endmethod
        
        method inc takes nothing returns nothing
            set .curr = .curr.next
        endmethod
        method operator value takes nothing returns integer
            return .curr.data
        endmethod
        method operator data takes nothing returns integer
            return .curr.data
        endmethod
        method operator hasNext takes nothing returns boolean
            return .curr != 0 and .curr.next != 0
        endmethod
        method operator isLast takes nothing returns boolean
            return .curr == 0 or .curr.next == 0
        endmethod
        method terminate takes nothing returns nothing
            set .curr = 0
        endmethod
        method destroy takes nothing returns nothing
        endmethod
    endmodule

endlibrary